package com.finanzas.servlet;

import java.io.IOException;
import org.json.simple.*;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.finanzas.dao.CatalogoDao;
import com.finanzas.dto.in.ConsultaCatalogoIn;
import com.finanzas.entity.Elemento;

/**
 * Servlet implementation class ConsultaCatalogoServlet
 */
@WebServlet("/ConsultaCatalogoServlet")
public class ConsultaCatalogoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB
	private CatalogoDao catalogoDao;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConsultaCatalogoServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		Map<String, String> catalogo = null;
		List<Map<String,String>>  list = new LinkedList<Map<String,String>>();
		Long idCatalogo = Long.parseLong(request.getParameter("idCatalogo"));
		ConsultaCatalogoIn in = new ConsultaCatalogoIn();
		in.setIdCatalogo(idCatalogo);
		List<Elemento> listCatalogo = catalogoDao.consultarCatalogo(in);
		if(listCatalogo!=null&&listCatalogo.size()>0){
			for(Elemento e:listCatalogo){
				catalogo = new LinkedHashMap<String, String>();
				catalogo.put("desc",e.getDescripcion().toUpperCase());
				catalogo.put("valor",e.getIdElemento().toString());
				list.add(catalogo);
			}
			
			String jSon = JSONValue.toJSONString(list);
			out.println(jSon);
		}else{
			out.print("Sin Existencia");
		}
	}

}
